package cn.imu.appdevexperiment2020;

import android.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.imu.appdevexperiment2020.FileUploadActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity{

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        //登录按钮
        Button loginButton = (Button) findViewById(R.id.loginButton);
        //注册按钮
        Button registerButton = (Button) findViewById(R.id.toregisterButton);
        //用户名文本框
        final EditText accountEditText = (EditText) findViewById(R.id.usernameEditText);
        //密码文本框
        final EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
        //设置登录按钮的点击事件
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取文本框内容
                System.out.println("*****");
                String username = accountEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();
                System.out.println(username);
                //接口URL
                String url = AppConfig.host + "/app/user/api/login";
                //将请求参数放入map封装，注意key值要与接口中给的参数名一致
                Map<String,String> param = new HashMap<>();
                param.put("userName", username);
                param.put("password", password);
                //使用Http工具类发送post请求
                System.out.println(param);
                HttpUtil.post(url, param, new Callback() {
                    //请求失败的回调方法
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "服务器通信失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                    //请求成功的回调方法
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //从response中获取响应体数据
                        final String responseData = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    //将响应体转换为jsonObject
                                    JSONObject jsonObject = new JSONObject(responseData);
                                    //判断响应码 200 为成功
                                    if (jsonObject.getString("code").equals("200")) {
                                        Toast.makeText(MainActivity.this, jsonObject.getString("nickName")+" 登录成功", Toast.LENGTH_SHORT).show();
                                        //调用主页面活动的跳转方法
                                        showMain();
                                    }
                                    else
                                    {
                                        Toast.makeText(MainActivity.this, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                });

            }
        });
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(MainActivity.this,
                        android.R.anim.fade_in, android.R.anim.fade_out).toBundle();
                startActivity(intent, bundle);
                finish();
            }
        });


    }

    private void showMain() {
        Intent intent = new Intent(MainActivity.this, FileUploadActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(MainActivity.this,
                android.R.anim.fade_in, android.R.anim.fade_out).toBundle();
        startActivity(intent, bundle);
        finish();
    }

}